package com.ruoyi.business.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.dto.OpenPositionSemaphoreDto;
import com.ruoyi.business.service.IBusinessTradeConfigService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusinessTradeConfig;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 交易参数设置Controller
 * 
 * @author ruoyi
 * @date 2024-04-17
 */
@RestController
@RequestMapping("/business/config")
public class BusinessTradeConfigController extends BaseController
{
    @Autowired
    private IBusinessTradeConfigService businessTradeConfigService;

    /**
     * 查询交易参数设置列表
     */
    @PreAuthorize("@ss.hasPermi('business:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusinessTradeConfig businessTradeConfig)
    {
        startPage();
        List<BusinessTradeConfig> list = businessTradeConfigService.selectBusinessTradeConfigList(businessTradeConfig);
        list = list.stream().peek(item->{
            String openPositionSemaphoreStr = item.getOpenPositionSemaphoreStr();
            if(StringUtils.isNotEmpty(openPositionSemaphoreStr)){
                item.setOpenPositionSemaphore(JSON.parseObject(openPositionSemaphoreStr,OpenPositionSemaphoreDto.class));

              ;
            }else{
                item.setOpenPositionSemaphore(new OpenPositionSemaphoreDto());
            }
        }).collect(Collectors.toList());
        return getDataTable(list);
    }

    /**
     * 导出交易参数设置列表
     */
    @PreAuthorize("@ss.hasPermi('business:config:export')")
    @Log(title = "交易参数设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessTradeConfig businessTradeConfig)
    {
        List<BusinessTradeConfig> list = businessTradeConfigService.selectBusinessTradeConfigList(businessTradeConfig);
        ExcelUtil<BusinessTradeConfig> util = new ExcelUtil<BusinessTradeConfig>(BusinessTradeConfig.class);
        util.exportExcel(response, list, "交易参数设置数据");
    }

    /**
     * 获取交易参数设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(businessTradeConfigService.selectBusinessTradeConfigById(id));
    }

    /**
     * 新增交易参数设置
     */
    @PreAuthorize("@ss.hasPermi('business:config:add')")
    @Log(title = "交易参数设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessTradeConfig businessTradeConfig)
    {
        return toAjax(businessTradeConfigService.insertBusinessTradeConfig(businessTradeConfig));
    }

    /**
     * 修改交易参数设置
     */
    @PreAuthorize("@ss.hasPermi('business:config:edit')")
    @Log(title = "交易参数设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusinessTradeConfig businessTradeConfig)
    {
        return toAjax(businessTradeConfigService.updateBusinessTradeConfig(businessTradeConfig));
    }

    /**
     * 删除交易参数设置
     */
    @PreAuthorize("@ss.hasPermi('business:config:remove')")
    @Log(title = "交易参数设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(businessTradeConfigService.deleteBusinessTradeConfigByIds(ids));
    }
}
